package com.example.zzl.viewmodel.HomeFragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;

import com.example.zzl.adapter.HomeFragment.NewsAdapter;
import com.example.zzl.custom.DividerItemDecoration;
import com.example.zzl.custom.SyLinearLayoutManager;
import com.example.zzl.databinding.HomeNewsBinding;
import com.example.zzl.model.HomeFragment.NewsModel;
import com.example.zzl.model.bean.Home;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ligg on 2017/2/10.
 */

public class NewsPresenter implements NewsModel.ICallbackM {

    private final NewsModel newsModel;
    private Context context;
    private NewsCallbackMv callback;
    private HomeNewsBinding db;
    private List<Home.News> list;
    private NewsAdapter adapter;



    public interface NewsCallbackMv {

    }

    public NewsPresenter(Context context, NewsCallbackMv callback) {
        this.context = context;
        this.callback = callback;
        newsModel = new NewsModel(this);
    }


    public void init() {
        list = new ArrayList<Home.News>();
        adapter = new NewsAdapter(context, list);
    }

    public void initView() {
        SyLinearLayoutManager layout = new SyLinearLayoutManager(context);
        layout.setOrientation(LinearLayoutManager.VERTICAL);
        db.rv.setLayoutManager(layout);
        db.rv.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL_LIST));
        db.rv.setAdapter(adapter);
    }


    public HomeNewsBinding getDb() {
        return db;
    }

    public void setDb(HomeNewsBinding db) {
        this.db = db;
    }

    @Override
    public void loadFinished() {
        list.clear();
        list.addAll(newsModel.getList());
        adapter.notifyDataSetChanged();
    }

    public void loadData() {
        newsModel.loadData();
    }
}
